package com.foo.designpattern.observer;

import java.util.ArrayList;

/**
 * com.foo.designpattern.observer
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-04-22 17:26.
 *
 * @Description 具体主题：旅行社
 */
public class TravelAgency implements Subject {

    String tourStartTime = null;
    String tourMess = null;
    ArrayList<Observer> personList = null;

    public TravelAgency() {
        this.personList = new ArrayList<Observer>();
    }

    @Override
    public void addObserver(Observer observer) {
        if ((observer != null) && (!personList.contains(observer))) {
            this.personList.add(observer);
        }
    }

    @Override
    public void delObserver(Observer observer) {
        if (observer != null) {
            this.personList.remove(observer);
        }
    }

    /**
     * 通知所有观察者
     */
    @Override
    public void notifyObservers() {
        for (int i = 0; i < this.personList.size(); i++) {
            Observer observer = this.personList.get(i);
            observer.update(this);
        }
    }

    /**
     * 新消息
     *
     * @param tourStartTime
     * @param tourMess
     */
    public void giveMess(String tourStartTime, String tourMess) {
        this.tourMess = tourMess;
        this.tourStartTime = tourStartTime;
        // 如果有新的消息则通知所有观察者
        this.notifyObservers();
    }

    public String getTourStartTime() {
        return tourStartTime;
    }

    public String getTourMess() {
        return tourMess;
    }

}
